package org.light.jpa.core;

import java.util.Iterator;
import java.util.Set;

import org.light.domain.Dao;
import org.light.domain.Domain;
import org.light.domain.Field;
import org.light.domain.Method;

public class JPADao extends Dao {
	
	@Override
	public String generateDaoString(){
		StringBuilder sb = new StringBuilder();
		if (this.packageToken !=null && !"".equalsIgnoreCase(this.packageToken)) sb.append("package ").append(this.packageToken).append("."+this.domain.getDaoSuffix()+";\n\n");
		Set<String> imports = this.generateImportStrings();
		for (String s: imports){
			sb.append("import ").append(s).append(";\n");
		}
		sb.append("import java.util.List;\n");
		sb.append("import org.springframework.data.jpa.repository.JpaRepository;\n");
		sb.append("import org.springframework.data.jpa.repository.Modifying;\n");
		sb.append("import org.springframework.data.jpa.repository.Query;\n");
		sb.append("import jakarta.transaction.Transactional;\n");
		sb.append("public interface ").append(this.getDomain().getStandardName()).append("Dao extends JpaRepository<").append(this.domain.getCapFirstDomainNameWithSuffix()).append(", Long>{\n");
		Iterator it = this.fields.iterator();
		while(it.hasNext()){
			Field f = (Field)it.next();
			sb.append("\tpublic ").append(f.getFieldType()).append(" ").append(f.getFieldName()).append(";\n");
		}
		Iterator it2 = this.methods.iterator();
		while(it2.hasNext()){
			Method m = (Method)it2.next();
			sb.append(m.generateMethodDefinition()).append("\n\n");
		}			
		sb.append("}\n");
		
		return sb.toString();
	}

	public JPADao(Domain domain){
		super();
		this.domain = domain;
		this.packageToken = domain.getPackageToken();
	}
}
